<template>
  <div class="coach-info">
    <el-form ref="dataForm" :model="coach" :rules="rules" label-width="100px" style="padding-right: 40px;">
      <el-form-item label="头像" prop="img">
        <el-upload class="avatar-uploader" action="/dev-api/admin/common/upload" :show-file-list="false"
                   :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <img v-if="coach.img" :src="coach.img" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>
      <el-form-item label="教练名" prop="coachName" style="width:400px">
        <el-input v-model="coach.coachName" />
      </el-form-item>
      <el-form-item label="性别" prop="sex" style="width:400px">
        <el-select v-model="coach.sex" placeholder="请选择">
          <el-option :key="0" label="男" :value="0" />
          <el-option :key="1" label="女" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="身份证" prop="idCard" style="width:400px">
        <el-input v-model="coach.idCard" />
      </el-form-item>
      <el-form-item label="教练电话" prop="phone" style="width:400px">
        <el-input v-model="coach.phone" />
      </el-form-item>

      <el-form-item label="门店" prop="storeId" style="width:400px">
        <el-select v-model="coach.storeId" placeholder="请选择门店">
          <el-option v-for="item in stores" :key="item.id" :label="item.storeName" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="个人介绍" prop="introduce" style="width:400px">
        <el-input v-model="coach.introduce" />
      </el-form-item>
      <el-form-item label="技能" prop="skill" style="width:400px">
        <el-input v-model="coach.skill" />
      </el-form-item>
      <el-form-item label="收费" prop="price" style="width:400px">
        <el-input v-model="coach.price" />
      </el-form-item>

      <!-- 证书和照片 -->
      <el-form-item label="资质证书" prop="cert">
        <el-upload action="/dev-api/admin/common/upload" list-type="picture-card" :file-list="coach.cert"
                   :on-success="handleAvatarSuccess1" :on-preview="handlePictureCardPreview"
                   :on-remove="handleRemove1">
          <i class="el-icon-plus" />
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item label="个人风采" prop="personal">
        <el-upload action="/dev-api/admin/common/upload" list-type="picture-card" :file-list="coach.personal"
                   :on-success="handleAvatarSuccess2" :on-preview="handlePictureCardPreview"
                   :on-remove="handleRemove2">
          <i class="el-icon-plus" />
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
            <el-button size="small" icon="el-icon-refresh-right" @click="cancel">取 消</el-button>
            <el-button type="primary" icon="el-icon-check" size="small" @click="saveOrUpdate('dataForm')">确
                定</el-button>
        </span>
  </div>
</template>

<script>
import api from '@/api/content/coach'
import storeApi from '@/api/content/store'

const defaultForm = {
  id: '',
  coachName: '',
  sex: '',
  phone: '',
  idCard: '',
  price: '',
  img: '',
  skill: '',
  introduce: '',
  storeId: '',
  cert: [],
  personal: []
}

export default {
  data() {
    return {
      coach: defaultForm,
      stores: [],

      dialogVisible: false,
      dialogImageUrl: '',

      rules: {
        coachName: [
          { required: true, message: '请填写姓名', trigger: 'blur' }
        ],
        idCard: [
          { required: true, message: '请填写身份证号码', trigger: 'blur' },
          { pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: '身份证号码格式不正确', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请填写电话', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        storeId: [
          { required: true, message: '请选择门店', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请填写个人介绍', trigger: 'blur' }
        ],
        skill: [
          { required: true, message: '请填写技能', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请填写收费', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.fetchStoreData()
    if (!this.$route.query.id) {
      this.coach = defaultForm
    } else {
      this.fetchData(this.$route.query.id)
    }
  },
  methods: {
    fetchData(id) {
      api.getById(id).then(
        response => {
          this.coach = response.data
        }
      )
    },
    // 加载门店列表数据
    fetchStoreData() {
      storeApi.getList().then(response => {
        this.stores = response.data
      })
    },
    // 添加或更新
    saveOrUpdate(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (!this.coach.id) {
            this.save(formName)
          } else {
            this.update(formName)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 添加
    save() {
      api.save(this.coach).then(response => {
        this.$message.success(response.message || '操作成功')
        this.$router.replace('/content/coach')
      })
    },
    // 更新
    update(formName) {
      api.updateById(this.coach).then(response => {
        this.$message.success(response.message || '操作成功')
        this.$router.replace('/content/coach')
      })
    },
    // 取消
    cancel() {
      this.$router.go(-1)
    },

    // 上传图片成功
    handleAvatarSuccess(res, file) {
      console.log(res)
      this.coach.img = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },

    handleAvatarSuccess1(response, file, fileList) {
      fileList[fileList.length - 1] = {
        name: file.name,
        url: response.data
      }
      this.coach.cert = fileList
    },
    handleRemove1(file, fileList) {
      this.coach.cert = fileList
    },
    handleAvatarSuccess2(response, file, fileList) {
      console.log(response)
      fileList[fileList.length - 1] = {
        name: file.name,
        url: response.data
      }
      this.coach.personal = fileList
    },
    handleRemove2(file, fileList) {
      this.coach.personal = fileList
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>
.coach-info {
  margin: 30px;
  margin-top: 40px;

  .dialog-footer {
    margin-left: 40px;
    margin-bottom: 20px;
  }
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}

.avatar {
  width: 128px;
  height: 128px;
  display: block;
}
</style>
